<?php ob_start();?>
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/checkUser.php');
$db = connect();
if(isset($_GET["ID"]) && $_GET["ID"] <> ''){
	if(isset($_GET["DELETE"])){
		$stmt ="DELETE FROM CATEGORY
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('i', $_GET["ID"]);
		if($statement->execute ()) {
			$statement->close();
			$stmt ="UPDATE CATEGORY
						SET CATEGORY_ORDER = CATEGORY_ORDER - 1
					WHERE CATEGORY_ORDER > ?";
			$statement = $db->prepare($stmt);
			$statement->bind_param('i', $_GET["ORDER"]);
			if(!$statement->execute ()) {
				echo 'İşlem sırasında hata alındı!';
			}
		} else {
			echo 'İşlem sırasında hata alındı !';
		}
		$statement->close();
	}else{
		$stmt ="UPDATE CATEGORY
						SET CATEGORY_ORDER = CATEGORY_ORDER - 1
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('i', $_GET["ID"]);
		if($statement->execute ()) {
			$statement->close();
			$stmt ="UPDATE CATEGORY
						SET CATEGORY_ORDER = CATEGORY_ORDER + 1
					WHERE ID = ?";
			$statement = $db->prepare($stmt);
			$statement->bind_param('i', $_GET["B_ID"]);
			if(!$statement->execute ()) {
				echo 'İşlem sırasında hata alındı';
			}
		}else {
			echo 'İşlem sırasında hata alındı';
		}
		$statement->close();
	}
}else{
	$stmt ="UPDATE CATEGORY
						SET CATEGORY_ORDER = CATEGORY_ORDER + 1
					WHERE CATEGORY_ORDER >= ?";
	$statement = $db->prepare($stmt);
	$statement->bind_param('i', $_GET["ORDER"]);
	if($statement->execute()){
		$statement->close();
		$stmt ="INSERT INTO CATEGORY (CATEGORY_NAME, CATEGORY_ORDER)
				VALUES(?,?)";
		$statement = $db->prepare($stmt);
		$statement->bind_param('si', $_GET['NAME'], $_GET['ORDER']);
		if(!$statement->execute ()) {
			echo 'İşlem sırasında hata alındı!';
		}else{
			echo $db->insert_id.'/'.$_GET['NAME'].'/'.$_GET['ORDER'];
		}
	} else {
		echo 'Kayıt sırasında hata alındı !';
	}
	$statement->close();
}
disconnect($db);
?>
<?php ob_end_flush(); ?>